// Lang_18 ['this' keyword].nova // The application class. class ThisKeywordApp { // Application class's "main" function. public static void main( String[] args ) { // Create a new "ThisKeyword" object. ThisKeyword t = new ThisKeyword( ); // Output the Integer object in the "ThisKeyword" object. Stream.writeLine( "t = " + Integer.toString( t.getObject( ).i ) ); // This "this" statement should produce an error. // "this" keywords are only allowed in instance methods (not static methods). // Uncomment this line to produce the error. // this; } } class ThisKeyword { // Instance attribute. public int i; // Class constructor. public ThisKeyword( ) { // Initialize the "i" attribute using the optional "this" keyword. this.i = 2003; // Output the value of "i". Stream.writeLine( "ThisKeyword.ThisKeyword( ) " + Integer.toString( this.i ) ); // Call the "call" method using the optional "this" keyword. this.call( ); // This "this" statement should produce an error. // You can't assign an object directly to the keyword. // this = new Test( ); } // Instance method. public void call( ) { Stream.writeLine( "ThisKeyword.call( ) method called." ); } // Method to return a reference to the main object. public ThisKeyword getObject( ) { return this; } }